// https://leetcode-cn.com/problems/gas-station/
// 134. 加油站

class Solution {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int total_sum = 0;
        int cur_sum = 0;
        int start = 0;

        for (int i = 0; i < gas.length; i++) {
            total_sum += gas[i] - cost[i];
            cur_sum += gas[i] - cost[i];

            if (cur_sum < 0) {
                start = i + 1;
                cur_sum = 0;
            }
        }

        if (total_sum < 0) {
            return -1;
        }
        return start;

    }
}